"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 基础功能
Case Name   : 开启发布订阅后，订阅端主备切换
Create At   : 2022/3/4
Owner       : opentestcase026
Description :
    1.两个集群创建表
    2.创建发布订阅
    3.更新数据
    4.查询数据是否更新
    5.订阅端主备切换
    6.更新数据
    7.查询数据是否更新
    8.恢复主备关系
    9.更新数据
    10.查询数据是否更新
Expect      :
    1.成功
    2.成功
    3.成功
    4.更新
    5.成功
    6.成功
    7.数据更新
    8.成功
    9.成功
    10.更新
History     :
    Modified by opentestcase001 2022/5/5:增加等待5.5s，适配最新代码，并删除guc参数还
    原步骤，减少用例执行耗时
    Modified by opentestcase004 2022/8/03:优化用例,增加基础数据同步功能后订阅端有基础
    数据的同时再在订阅端修改表数据，主键冲突，关闭基础数据同步
    Modified by opentestcase012 2022/10/12:优化用例和断言,添加查看数据库状态
"""
import unittest
import os
from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.sta_userdb_pub = Node(node='Standby1DbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.sta_userdb_sub = Node(node='remote1_Standby1DbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.commsh_sub_sta = CommonSH('remote1_Standby1DbUser')
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.tb_name1 = 'tb_pubsub_case062_1'
        self.tb_name2 = 'tb_pubsub_case062_2'
        self.subname = "sub_case062_1"
        self.pubname = "pub_case062_1"
        self.hba_pub = os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')
        self.hba_sub = os.path.join(macro.DB_INSTANCE_PATH_REMOTE1,
                                    'pg_hba.conf')
        self.hba_bak_pub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'pg_hba.conf')
        self.hba_bak_sub = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1), 'pg_hba.conf')
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.enable_slot_log_pub = self.com_pub.show_param("enable_slot_log")
        self.enable_slot_log_sub = self.com_sub.show_param(
            "enable_slot_log", macro.DB_ENV_PATH_REMOTE1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
            f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
            f'-W {self.pri_userdb_sub.db_password}'
        self.hostname = self.com_pub.get_sh_result(self.pri_userdb_sub,
                                                   'hostname')

        text = '-----预置条件1:备份配置文件pg_hba.conf expect:状态正常-----'
        self.log.info(text)
        cmd = f'cp {self.hba_pub} {self.hba_bak_pub};'
        self.log.info(cmd)
        msg1 = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'cp {self.hba_sub} {self.hba_bak_sub};'
        self.log.info(cmd)
        msg2 = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)
        self.assertEqual('', msg1 and msg2, '执行失败:' + text)

        text = '-----预置条件2:查看发布端和订阅端的数据库状态 expect:状态正常-----'
        self.log.info(text)
        status = self.commsh_pub.get_db_cluster_status()
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status, '执行失败:' + text)
        status = self.commsh_sub.get_db_cluster_status(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status, '执行失败:' + text)

        text = '-----预置条件3:修改发布端pg_hba文件和配置参数wal_level,重启数据库 expect:成功-----'
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.sta_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
            self.assertTrue(result, '执行失败:' + text)
            result = self.commsh_pub.restart_db_cluster(False)
            self.assertTrue(result, '执行失败:' + text)
            status = self.commsh_pub.get_db_cluster_status()
            self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status,
                          '执行失败:' + text)

        text = '-----预置条件4:订阅端修改pg_hba文件 expect:成功-----'
        self.log.info(text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.sta_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败:' + text)

    def test_pubsub(self):
        text = '--step:两个集群均创建表 expect:成功--'
        self.log.info(text)
        sql = f"drop table if exists {self.tb_name1};" \
            f"create table {self.tb_name1}(id1 int primary key, " \
            f"id2 int, id3 int);" \
            f"drop table if exists {self.tb_name2};" \
            f"create table {self.tb_name2}(id int primary key " \
            f"constraint id_nn not null,use_filename varchar2(20)," \
            f"filename varchar2(255),text varchar2(2000))" \
            f"partition by range(id)(        " \
            f"partition p1 values less than(30),        " \
            f"partition p2 values less than(60),        " \
            f"partition p3 values less than(90),        " \
            f"partition p4 values less than(maxvalue));"
        result = self.commsh_pub.execut_db_sql(sql,
                                               self.user_param_pub)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.CREATE_TABLE_SUCCESS),
                         4, '执行失败:' + text)
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.CREATE_TABLE_SUCCESS),
                         4, '执行失败:' + text)

        text = '--step2:创建发布订阅 expect:成功--'
        self.log.info(text)
        result = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, 'enable_slot_log=on')
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'enable_slot_log=on',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        sql = f"drop publication if exists {self.pubname};" \
            f"create publication {self.pubname} for all tables;"
        result = self.commsh_pub.execut_db_sql(sql,
                                               self.user_param_pub)
        self.log.info(result)
        self.assertIn(self.constant.create_pub_succ_msg, result,
                      '执行失败:' + text)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result,
                         '执行失败:' + text)
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn('', result, '执行失败:' + text)
        result = self.commsh_sub_sta.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn('', result, '执行失败:' + text)
        sql = f"drop subscription if exists {self.subname};" \
            f"create subscription {self.subname} connection " \
            f"'host={self.pri_userdb_pub.db_host} " \
            f"port={self.port} " \
            f"user={self.pri_userdb_pub.db_user} " \
            f"dbname={self.pri_userdb_pub.db_name} " \
            f"password={self.pri_userdb_pub.ssh_password}' " \
            f"publication {self.pubname} with (copy_data=false);"
        result = self.commsh_sub.execut_db_sql(sql, self.user_param_sub, None,
                                               macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertIn(self.constant.create_sub_succ_msg,
                      result, '执行失败:' + text)

        text = '--step3:更新数据 expect:成功--'
        self.log.info(text)
        sql_modify = f"insert into {self.tb_name1} values(1, 1, 1),(2,2,2);" \
            f"insert into {self.tb_name2} values(1, 'first', '%一', '')," \
            f"(60, 'first', '%二', ''),(90, 'first', '%三', ''); " \
            f"select pg_sleep(5.5);"
        result = self.commsh_pub.execut_db_sql(sql_modify, self.user_param_pub)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.INSERT_SUCCESS_MSG),
                         2, '执行失败:' + text)

        text = '--step4:查询数据是否更新  expect:更新'
        self.log.info(text)
        sql_select = f"select * from {self.tb_name1};" \
            f"select * from {self.tb_name2};"
        result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertEqual(result.count('2 rows'), 1, '执行失败:' + text)
        self.assertEqual(result.count('3 rows'), 1, '执行失败:' + text)

        text = '--step5:订阅端备升主  expect:成功'
        self.log.info(text)
        result = self.commsh_sub.stop_db_instance(
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG,
                      result, '执行失败:' + text)
        result = self.commsh_sub_sta.execute_gsctl(
            'failover', self.constant.FAILOVER_SUCCESS_MSG,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub_sta.exec_refresh_conf(
            macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)

        text = '--step6:更新数据 expect:成功--'
        self.log.info(text)
        sql_modify = f"delete from {self.tb_name1} where id1=1;" \
            f"delete from {self.tb_name2} where id=1; "
        result = self.commsh_pub.execut_db_sql(sql_modify, self.user_param_pub)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.DELETE_SUCCESS_MSG),
                         2, '执行失败:' + text)

        text = '--step7:查询数据是否更新  expect:数据更新--'
        self.log.info(text)
        result = self.commsh_sub_sta.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertEqual(result.count('2 rows'), 1, '执行失败:' + text)
        self.assertEqual(result.count('1 row'), 1, '执行失败:' + text)
        self.assertNotIn('1 |   1 |   1', result, '执行失败:' + text)
        self.assertNotIn('1 | first        | %一     |', result, '执行失败:' +
                         text)

        text = '--step8:恢复主备关系  expect:成功'
        self.log.info(text)
        result = self.commsh_sub.start_db_instance(
            'standby',
            macro.DB_ENV_PATH_REMOTE1,
            macro.DB_INSTANCE_PATH_REMOTE1)
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, result, '执行失败:' +
                      text)
        result = self.commsh_sub.execute_gsctl(
            'switchover', self.constant.SWITCHOVER_SUCCESS_MSG,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)
        result = self.commsh_sub.exec_refresh_conf(macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(result, '执行失败:' + text)

        text = '--step9:更新数据 expect:成功--'
        self.log.info(text)
        sql_modify = f"update {self.tb_name1} set id3=999;" \
            f"update {self.tb_name2} set id=999 where id=90;"
        result = self.commsh_pub.execut_db_sql(sql_modify, self.user_param_pub)
        self.log.info(result)
        self.assertEqual(result.count(self.constant.UPDATE_SUCCESS_MSG),
                         2, '执行失败:' + text)

        text = '--step10:查询数据是否更新  expect:更新--'
        self.log.info(text)
        result = self.commsh_sub.execut_db_sql(
            sql_select, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        self.assertEqual(result.count('1 row'), 1, '执行失败:' + text)
        self.assertEqual(result.count('2 rows'), 1, '执行失败:' + text)
        self.assertIn('2 |   2 | 999', result, '执行失败:' + text)
        self.assertIn('999 | first        | %三     |',
                      result, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '-----订阅端数据库主备切换恢复初始状态 expect:成功-----'
        self.log.info(text1)
        result = self.commsh_sub.check_whether_need_switch(
            self.hostname, envpath=macro.DB_ENV_PATH_REMOTE1)
        result_switch = True
        result_refresh = True
        if result:
            self.commsh_sub.start_db_instance(
                'standby',
                macro.DB_ENV_PATH_REMOTE1,
                macro.DB_INSTANCE_PATH_REMOTE1)
            result_switch = self.commsh_sub.execute_gsctl(
                'switchover', self.constant.SWITCHOVER_SUCCESS_MSG,
                env_path=macro.DB_ENV_PATH_REMOTE1,
                dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
            self.log.info(result_switch)
            result_refresh = self.commsh_sub.exec_refresh_conf(
                macro.DB_ENV_PATH_REMOTE1)
            self.log.info(result_refresh)

        text2 = '-----删除发布订阅 expect:成功-----'
        self.log.info(text2)
        sql = f"drop subscription if exists {self.subname};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql = f"drop publication if exists {self.pubname};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)

        text3 = '-----订阅端和发布端删除表 expect:成功-----'
        self.log.info(text3)
        sql = f"drop table if exists {self.tb_name1} cascade;" \
            f"drop table if exists {self.tb_name2} cascade;"
        sql_drop_sub = self.commsh_sub.execut_db_sql(sql, self.user_param_sub,
                                                     None,
                                                     macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sql_drop_sub)
        sql_drop_pub = self.commsh_pub.execut_db_sql(
            sql, sql_type=self.user_param_pub)
        self.log.info(sql_drop_pub)

        text4 = '-----订阅端和发布端恢复pg_hba.conf文件 expect:成功-----'
        self.log.info(text4)
        cmd = f'if [[ -e {self.hba_pub} && -e {self.hba_bak_pub} ]]; ' \
            f'then rm -rf {self.hba_pub}; ' \
            f'mv {self.hba_bak_pub} {self.hba_pub}; ' \
            f'else echo not exits; fi'
        hba_msg_pub = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
        cmd = f'if [[ -e {self.hba_sub} && -e {self.hba_bak_sub} ]]; ' \
            f'then rm -rf {self.hba_sub}; ' \
            f'mv {self.hba_bak_sub} {self.hba_sub}; ' \
            f'else echo not exits; fi'
        hba_msg_sub = self.com_pub.get_sh_result(self.pri_userdb_sub, cmd)

        text5 = '-----订阅端和发布端恢复参数配置 expect:成功-----'
        self.log.info(text5)
        guc_wal_pub = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        guc_slot_pub = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_slot_log={self.enable_slot_log_pub}')
        guc_slot_sub = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_slot_log={self.enable_slot_log_sub}',
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)

        text6 = '-----重启数据库查看数据库状态 expect:成功-----'
        self.log.info(text6)
        res_msg_pub = self.commsh_pub.restart_db_cluster(False)
        status_pub = self.commsh_pub.get_db_cluster_status()
        self.log.info(status_pub)
        res_msg_sub = self.commsh_sub.restart_db_cluster(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        status_sub = self.commsh_sub.get_db_cluster_status(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(status_sub)

        self.assertTrue(result_refresh and result_switch, '执行失败:' + text1)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text2)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text2)
        self.assertEqual(2, sql_drop_sub.count(
            self.constant.TABLE_DROP_SUCCESS), '执行失败' + text3)
        self.assertEqual(2, sql_drop_pub.count(
            self.constant.TABLE_DROP_SUCCESS), '执行失败' + text3)
        self.assertEqual('', hba_msg_pub and hba_msg_sub, '执行失败:' + text4)
        self.assertTrue(guc_wal_pub and guc_slot_pub and guc_slot_sub,
                        '执行失败:' + text5)
        self.assertTrue(res_msg_pub and res_msg_sub, '执行失败:' + text6)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_pub,
                      '执行失败:' + text6)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_sub,
                      '执行失败:' + text6)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
